import os

from datetime import datetime


class FileLog(object):
    def __init__(self, log_dir: str):
        # get log dir & path
        self.mLogDir = log_dir
        self.mLogPath = os.path.join(self.mLogDir, datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

        # check log path
        if os.path.exists(self.mLogPath):
            c = 2
            original_path = self.mLogPath
            while os.path.exists(self.mLogPath):
                self.mLogPath = '%s_%d' % (original_path, c)
                c += 1

    def log(self, msg: str):
        if not os.path.exists(self.mLogDir):
            os.makedirs(self.mLogDir)

        with open(self.mLogPath, 'a') as file:
            file.write(datetime.now().strftime('[%Y-%m-%d %H:%M:%S] '))
            file.write(msg)
            file.write('\n')
